#include "map_of_quality.h"
#include <qwt_plot_spectrogram.h>
#include <qwt_scale_widget.h>
#include <qwt_scale_draw.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_marker.h>
#include <qwt_color_map.h>
#include "spectrogram_data.h"

MapOfQuality::MapOfQuality(QWidget *parent):
        Plot(parent)
{

}

void MapOfQuality::plotMap(QVector< QVector<double> > matrizQual,double limiteinfx, double limiteinfy, double limitesupx, double limitesupy)
{
    double menorvaluex = 620000;
    double maiorvaluex = 624000;
    double menorvaluey = 8680000;
    double maiorvaluey = 8684000;
    this->spectrogram = new QwtPlotSpectrogram();
    QwtLinearColorMap colorMap(QColor(139,142,247),QColor(240,142,19));
    colorMap.addColorStop(0.35, QColor(194,234,182));
    colorMap.addColorStop(0.70, QColor(234,224,67));
    this->spectrogram->setColorMap(colorMap);
    this->spectrogram->setData(SpectrogramData(matrizQual,100,limiteinfx,limiteinfy));


    this->spectrogram->attach(this);
    QwtValueList contourLevels;
    for ( double level = 0.5; level < 100.0; level += 1.0 )
        contourLevels += level;
    this->spectrogram->setContourLevels(contourLevels);

    // A color bar on the right axis
    QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
    rightAxis->setTitle("FUZZY");
    rightAxis->setColorBarEnabled(true);
    rightAxis->setColorMap(this->spectrogram->data().range(),
        this->spectrogram->colorMap());

    setAxisScale(QwtPlot::yRight,
        this->spectrogram->data().range().minValue(),
        this->spectrogram->data().range().maxValue() );
    enableAxis(QwtPlot::yRight);

   //ESCOLHER A ESCALA DO EIXO X
   /////setAxisScale(QwtPlot::xBottom,menorvaluex - 150,maiorvaluex + 150,qRound((maiorvaluex - menorvaluex)/4));
   //setAxisScale(QwtPlot::xBottom,menorvaluex,maiorvaluex,qRound((maiorvaluex - menorvaluex)/4));
   setAxisScale(QwtPlot::xBottom,menorvaluex,maiorvaluex,qRound((limitesupx - limiteinfx)/4));

   enableAxis(QwtPlot::xBottom);

   //ESCOLHER A ESCALA DO EIXO Y
    /////setAxisScale(QwtPlot::yLeft,menorvaluey - 150,maiorvaluey + 150,qRound((maiorvaluey - menorvaluey)/10));
    setAxisScale(QwtPlot::yLeft,menorvaluey,maiorvaluey,qRound((limitesupy - limiteinfy)/10));

    enableAxis(QwtPlot::yLeft);

    QwtPlotZoomer* zoomer = new MyZoomer(canvas());
#if QT_VERSION < 0x040000
    zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
        Qt::RightButton, Qt::ControlButton);
#else
    zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
        Qt::RightButton, Qt::ControlModifier);
#endif
    zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
        Qt::RightButton);

    QwtPlotPanner *panner = new QwtPlotPanner(canvas());
    panner->setAxisEnabled(QwtPlot::yRight, false);
    panner->setMouseButton(Qt::MidButton);

    const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
    QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
    sd->setMinimumExtent( fm.width("100.00") );

    const QColor c(Qt::darkBlue);
    zoomer->setRubberBandPen(c);
    zoomer->setTrackerPen(c);
    replot();

}
